
package com.yuan.graduate;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.AMap.OnMapLongClickListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.yuan.application.BaseActivity;
import com.yuan.dialog.CustomProgressDialog;
import com.yuan.dialog.DialogUtil;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.ToastHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

/**
 * 地图界面
 * 
 * @author YUAN
 */
@SuppressLint("HandlerLeak")
public class MapActivity extends BaseActivity implements
        OnMapLongClickListener, OnCameraChangeListener, OnClickListener {
    private Context mContext = null;
    private MapView mapView = null;
    private AMap aMap = null;
    private TextView t_map_city = null;
    private EditText et_place = null;
    private Button b_map_search = null;
    private String lat = null, lng = null;
    private CustomProgressDialog progessdialog = null;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {// 此方法在ui线程运行
            switch (msg.what) {
                case CITYCHOICE:
                    String city = msg.getData().getString("city");
                    t_map_city.setText(city);
                    break;
                case MSG_GETGPSFROMNAME:
                    if (progessdialog != null)
                    {
                        progessdialog.dismiss();
                    }
                    @SuppressWarnings("unchecked")
                    List<GeocodeAddress> listadress = (List<GeocodeAddress>) msg.obj;
                    drawtomap(listadress);
                    break;
                case MSG_GETNAMEFROMGPS:
                    if (progessdialog != null)
                    {
                        progessdialog.dismiss();
                    }
                    String str = (String) msg.obj;
                    if (str == null || str.equals("null") || str.length() < 1)
                    {
                        ToastHelper.getInstance().shortToast(mContext, R.string.no_adress);
                        return;
                    }
                    DialogUtil.createWorkAdressDialog(mContext, str, lat, lng).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState);// 必须要写
        mContext = this;
        initView();

    }

    private void initView() {
        // TODO Auto-generated method stub
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        aMap.setOnMapLongClickListener(this);// 对amap添加长按地图事件监听器
        aMap.setOnCameraChangeListener(this);// 对amap添加移动地图事件监听器
        ToastHelper.getInstance().shortToast(mContext, "长按地图进行设置");
        t_map_city = (TextView) findViewById(R.id.t_map_city);
        et_place = (EditText) findViewById(R.id.et_place);
        b_map_search = (Button) findViewById(R.id.b_map_search);
        b_map_search.setOnClickListener(this);
        t_map_city.setOnClickListener(this);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        mContext = null;
        mapView = null;
        aMap = null;
        t_map_city = null;
        et_place = null;
        b_map_search = null;
        mHandler = null;
        lat = null;
        lng = null;
        progessdialog = null;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.t_map_city:
                DialogUtil.createCityChoiceDialog(mContext, mHandler,
                        t_map_city.getText().toString()).show();
                break;
            case R.id.b_map_search:
                String adress = et_place.getText().toString().trim();
                if (adress.length() < 1)
                {
                    ToastHelper.getInstance().shortToast(mContext, R.string.no_input);
                    return;
                }
                progessdialog = new CustomProgressDialog(mContext);
                progessdialog.setUpdateMsg(R.string.searching_place);
                progessdialog.show();
                AsyncDataJobHandler.getInstance().getGPSFromName(mHandler, adress,
                        t_map_city.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public void onCameraChange(CameraPosition arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCameraChangeFinish(CameraPosition arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMapLongClick(LatLng arg0) {
        // TODO Auto-generated method stub
        LatLonPoint point = new LatLonPoint(arg0.latitude, arg0.longitude);
        progessdialog = new CustomProgressDialog(mContext);
        progessdialog.setUpdateMsg(R.string.getting_place_info);
        progessdialog.show();
        AsyncDataJobHandler.getInstance().getNameFromGPS(mHandler, point);
        lat = String.valueOf(arg0.latitude);
        lng = String.valueOf(arg0.longitude);
    }

    @Override
    public void onMyDestroy()
    {
        mContext = null;
        mapView = null;
        aMap = null;
        t_map_city = null;
        et_place = null;
        b_map_search = null;
        mHandler = null;
        lat = null;
        lng = null;
        progessdialog = null;
        super.onMyDestroy();
    }

    private void drawtomap(List<GeocodeAddress> listadress) {
        aMap.clear();
        MarkerOptions mo = null;
        LatLonPoint latlngpoint = null;
        LatLng latlng = null;
        if (listadress == null || listadress.size() < 1) {
            ToastHelper.getInstance().shortToast(mContext,
                    mContext.getString(R.string.no_adress));
            return;
        } else {
            for (GeocodeAddress address : listadress) {
                latlngpoint = address.getLatLonPoint();
                latlng = new LatLng(latlngpoint.getLatitude(),
                        latlngpoint.getLongitude());
                mo = new MarkerOptions();
                mo.title(address.getFormatAddress());
                mo.position(latlng);
                aMap.addMarker(mo);
            }
            latlngpoint = listadress.get(0).getLatLonPoint();
            latlng = new LatLng(latlngpoint.getLatitude(),
                    latlngpoint.getLongitude());
            CameraUpdate cameraupdate = CameraUpdateFactory.newLatLngZoom(
                    latlng, 5);
            aMap.animateCamera(cameraupdate);
        }
    }

}
